const tsCompiler = require("typescript"); // TS编译器

// 待分析代码片段字符串
const tsCode = `import { app } from 'framework';                    

const dataLen = 3;
let name = 'iceman';

if(app){
    console.log(name);
}

function getInfos (info: string) {
    const result = app.get(info);
    return result;
}`;

//
let apiMap = {};
// 获取AST
const ast = tsCompiler.createSourceFile(
    "xxx",
    tsCode,
    tsCompiler.ScriptTarget.Latest,
    true
);


function walk(node) {
    tsCompiler.forEachChild(node, walk);
    if (tsCompiler.isIdentifier(node) && node.escapedText === "app") {

        const curLine = ast.getLineAndCharacterOfPosition(node.getStart()).line + 1;
        if(curLine===1){
            return
        }
        if (apiMap?.[node.escapedText]) {
            apiMap[node.escapedText].callLine.push(curLine);
            apiMap[node.escapedText].callCount++;
        } else {
            apiMap[node.escapedText] = {
                callLine: [curLine],
                callCount: 1,
            };
        }
    }
}

walk(ast);
console.log(apiMap)
